VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "classINI"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'******************************'
'******************************'

' INI Class V1.1
' http://iceyboard.no-ip.org/projects/code/vb6/ini_class/

' INI Class is Copyright 2005-2006 Icey
'
' This program is free software; you can redistribute it and/or
'   modify it under the terms of the GNU General Public License
'   as published by the Free Software Foundation; either version
'   2 of the License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty
'   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
'   the GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public
'   License along with this program; if not, write to the Free
'   Software Foundation, Inc., 51 Franklin St, Fifth Floor,
'   Boston, MA 02110-1301 USA

'******************************'
'******************************'

' all variables are declared
Option Explicit

' this is for getting data from an ini file
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
  "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, _
  ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, _
  ByVal lpFileName As String) As Long
' this is used to obtains sections
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
  "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
  ByVal nSize As Long, ByVal lpFileName As String) As Long
' this gets all the sections in an ini file
Private Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias _
  "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, _
  ByVal lpFileName As String) As Long
' this is for saving data to an ini file
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
  "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
  ByVal lpString As Any, ByVal lpFileName As String) As Long
' this makes new sections in the ini file
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias _
  "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, _
  ByVal lpFileName As String) As Long

' the name of the settings file
Dim strINIFile As String

' this stores the path and filename of the ini file
Public Sub ic_UseINI(ByVal strFilename As String)
    ' save to variable
    strINIFile = strFilename
End Sub

' this sub routine is responsible for writing data to the ini file
Public Sub ic_WriteString(ByVal Sec As String, ByVal Key As String, ByVal Value As String)
    ' this holds the data returned from the dll call
    Dim lngReturn As Long
    ' write the data to the file
    lngReturn = WritePrivateProfileString(Sec, Key, Value, strINIFile)
End Sub

' this sub routine is responsible for reading data from the ini file
Public Function ic_GetString(ByVal Sec As String, ByVal Key As String) As String
    ' this will hold the data from the call
    Dim sRet As String
    ' fill with blank spaces
    sRet = String(4096, Chr(0))
    ' find the data and return it to the function that called this one
    ic_GetString = Left(sRet, GetPrivateProfileString(Sec, ByVal Key, vbNullString, sRet, 5000, strINIFile))
End Function

' this sub routine is responsible for deleting data in the ini file
Public Sub ic_DeleteString(ByVal Sec As String, ByVal Key As String)
    ' delete the string
    WritePrivateProfileString Sec, Key, Nothing, strINIFile
End Sub

' this function returns the sections of the ini file in an array
Public Function ic_GetSections() As String()
    ' declare variables for returning data
    Dim strBuffer As String
    Dim intReturn As Integer
    ' the section names will be stored in this variable
    strBuffer = Space(4096)
    ' get the section names and returned the length of the joined section name array
    intReturn = GetPrivateProfileSectionNames(strBuffer, Len(strBuffer), strINIFile)
    ' just get the actual data returned from the call
    ' split the section names using the null chars
    ic_GetSections = Split(Left(strBuffer, intReturn), vbNullChar)
End Function

' gets all the string names in a section
' returns each one in format [string]=[value]
Public Function ic_GetSectionStrings(ByVal strSectionName As String) As String()
    ' declare variables for returning data
    Dim strBuffer As String
    Dim intReturn As Integer
    ' the section names will be stored in this variable
    strBuffer = Space(8192)
    ' get the section names and returned the length of the joined section name array
    intReturn = GetPrivateProfileSection(strSectionName, strBuffer, Len(strBuffer), strINIFile)
    ' just get the actual data returned from the call
    ' split the section names using the null chars
    ic_GetSectionStrings = Split(Left(strBuffer, intReturn), vbNullChar)
End Function

' deletes a section from the ini file
Public Sub ic_DeleteSection(ByVal strSectionName As String)
    ' this removes all keys/values and the section header
    WritePrivateProfileString strSectionName, Nothing, Nothing, strINIFile
End Sub

' this creates just the header of the section
Public Sub ic_MakeSection(ByVal strSectionName As String)
    ' add the section to the ini file
    WritePrivateProfileSection strSectionName, "", strINIFile
End Sub

' copy a section to somewhere else
Public Function ic_CopySection(ByVal strSectionName As String, ByVal strNewSectionName As String)
    ' declare variables for returning data
    Dim strBuffer As String
    Dim intReturn As Integer
    ' the section names will be stored in this variable
    strBuffer = Space(4096)
    ' get the section names and returned the length of the joined section name array
    intReturn = GetPrivateProfileSection(strSectionName, strBuffer, Len(strBuffer), strINIFile)
    ' write a new section based on this data
    WritePrivateProfileSection strNewSectionName, Left(strBuffer, intReturn), strINIFile
End Function

' cut a section to somewhere else
Public Function ic_CutSection(ByVal strSectionName As String, ByVal strNewSectionName As String)
    ' copy
    ic_CopySection strSectionName, strNewSectionName
    ' delete
    ic_DeleteSection strSectionName
End Function



